package main

import (
	"gin-capnprotodemo/rpc"
	"log"
	"os"
)

func main() {
	title := "War and Peace"
	pageCount := int32(1440)

	log.Printf("Encoding book message: (title: '%s', pageCount: %d) ...", title, pageCount)
	data, err := rpc.EncodeBookMessage(title, pageCount)
	if err != nil {
		log.Fatalf("Encoding book message: (title: '%s', pageCount: %d) failed: %s", title, pageCount, err)
	}

	log.Printf("Book message: %#v, len: %d", data, len(data))

	filePath := "book.msg"
	log.Printf("Writing book message to %s ...", filePath)
	if err := os.WriteFile(filePath, data, 0644); err != nil {
		log.Fatalf("Writing book message to %s failed: %s", filePath, err)
	}

	log.Print("Decoding book message ...")
	title, pageCount, err = rpc.DecodeBookMessage(data)
	if err != nil {
		log.Printf("Decoding book message failed: %s", err)
	}

	log.Printf("Decoded book message: (title: '%s', pageCount: %d)", title, pageCount)
}
